package com.example.run_game;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpStatus;
import org.xml.sax.SAXException;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

public class WeatherManager
{
	private PreRunMenu prerunmenu;
	
	private String weather_type;
	private String weather_date;
	private String temperature;
	private String wind_direction;
	private String wind_speed;
	private String weather_code;
	private boolean ready = false;
	
	private Handler handler;

	WeatherManager(Handler h)
	{
		handler = h;
		new ASyncWeatherQuery().execute();		
	}
	
	private class ASyncWeatherQuery extends AsyncTask<Void, Void, String[]>
	{
		@Override
		protected String[] doInBackground(Void... arg0)
		{
			 String[] returnArray = {"","","","","",""};	
	    	 try 
	    	 {
	    		 System.out.println("GOT TO THE ASYNC TASK #####################");
	    	     URL url = new URL("http://weather.yahooapis.com/forecastrss?w=26475870");
	    		 URLConnection connection = url.openConnection();
	    		 HttpURLConnection httpConn = (HttpURLConnection)connection;
	    		 httpConn.setDoInput(true);
	    		 httpConn.setRequestProperty("charset", "utf-8");
	    		 int responseCode = httpConn.getResponseCode();
	    		 System.out.println("RESPONSE CODE: "+responseCode);
	    		 if(responseCode == HttpStatus.SC_OK) {
	    		     InputStream xmlStream = httpConn.getInputStream();
	    		     System.out.println("GOT TO THE INPUTSTREAM #####################");
	    		     // Pass the xmlStream object to a XPP, SAX or DOM parser.
	    		     XMLParser parser = new XMLParser();
	    		     returnArray = parser.parseXMLWeather(xmlStream);
	    		     /*
	    		      * Uncomment to print out the xml from the url
	    		      * 
	    		     BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
	    		     String line = null;
	    		     while((line = in.readLine()) != null) {
	    		       System.out.println(line);
	    		     }
	    		     */
	    		}
				//dist = gMapDistQuery.getJSONObject("rows").getJSONObject("distance").getInt("value");
			 }
			 catch (MalformedURLException e) {
	             e.printStackTrace();
	         } catch (IOException e) {
	             e.printStackTrace();
	 		 }	
			 catch (android.os.NetworkOnMainThreadException e) {
				e.printStackTrace();
			 } catch (SAXException e)
			{
				e.printStackTrace();
			}	
	    	 
	    	 return returnArray;
	     }

		@Override
		protected void onPostExecute(String[] result)
		{
			weather_date = result[0];
			weather_type = result[1];
			temperature = result[2];
			wind_direction = result[3];
			wind_speed = result[4];
			weather_code = result[5];
			System.out.println(weather_date + "---"+weather_type+ "---"+temperature+ "---"+wind_direction+ "---"+wind_speed);
			//ready = true;
			//handler = prerunmenu.getWeatherHandler();
			//System.out.println("GOT THE HANDLER FROM PRERUNMENU");
			Message.obtain(handler, 2, result).sendToTarget();
		}
   	     
	    // protected int onPostExecute(int result) {
	         //showDialog("Downloaded " + result + " bytes");
	     //}

	 }

	
	
	public String getWeather_code()
	{
		return weather_code;
	}

	public void setWeather_code(String weather_code)
	{
		this.weather_code = weather_code;
	}

	public String getWeather_type()
	{
		return weather_type;
	}

	public void setWeather_type(String weather_type)
	{
		this.weather_type = weather_type;
	}

	public String getWeather_date()
	{
		return weather_date;
	}

	public void setWeather_date(String weather_date)
	{
		this.weather_date = weather_date;
	}

	public String getTemperature()
	{
		return temperature;
	}

	public void setTemperature(String temperature)
	{
		this.temperature = temperature;
	}

	public String getWind_direction()
	{
		return wind_direction;
	}

	public void setWind_direction(String wind_direction)
	{
		this.wind_direction = wind_direction;
	}

	public String getWind_speed()
	{
		return wind_speed;
	}

	public void setWind_speed(String wind_speed)
	{
		this.wind_speed = wind_speed;
	}
	
	
}
